package com.eshop.tools.service.impl;

import org.springframework.beans.BeanUtils;
import java.io.IOException;
import java.util.Iterator;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import com.eshop.utils.StringUtils;
import com.eshop.exception.BadRequestException;
import cn.hutool.core.util.ObjectUtil;
import java.io.File;
import com.eshop.utils.FileUtil;
import org.springframework.web.multipart.MultipartFile;
import java.io.Serializable;
import java.util.List;
import com.eshop.tools.service.dto.LocalStorageDto;
import java.util.LinkedHashMap;
import com.github.pagehelper.PageInfo;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.eshop.common.utils.QueryHelpPlus;
import java.util.Map;
import org.springframework.data.domain.Pageable;
import com.eshop.tools.service.dto.LocalStorageQueryCriteria;
import org.springframework.beans.factory.annotation.Value;
import com.eshop.dozer.service.IGenerator;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;
import com.eshop.tools.service.LocalStorageService;
import com.eshop.tools.domain.LocalStorage;
import com.eshop.tools.service.mapper.LocalStorageMapper;
import com.eshop.common.service.impl.BaseServiceImpl;

@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = { Exception.class })
public class LocalStorageServiceImpl extends BaseServiceImpl<LocalStorageMapper, LocalStorage> implements LocalStorageService
{
    private final IGenerator generator;
    @Value("${file.path}")
    private String path;
    @Value("${file.maxSize}")
    private long maxSize;
    
    public LocalStorageServiceImpl(final IGenerator generator) {
        this.generator = generator;
    }
    
    public Map<String, Object> queryAll(final LocalStorageQueryCriteria criteria, final Pageable pageable) {
        this.getPage(pageable);
        final PageInfo<LocalStorage> page = (PageInfo<LocalStorage>)new PageInfo(((LocalStorageMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(LocalStorage.class, criteria)));
        final Map<String, Object> map = new LinkedHashMap<String, Object>(2);
        map.put("content", this.generator.convert(page.getList(), (Class)LocalStorageDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }
    
    public List<LocalStorageDto> queryAll(final LocalStorageQueryCriteria criteria) {
        return (List<LocalStorageDto>)this.generator.convert(((LocalStorageMapper)this.baseMapper).selectList((Wrapper)QueryHelpPlus.getPredicate(LocalStorage.class, criteria)), (Class)LocalStorageDto.class);
    }
    
    public LocalStorageDto findById(final Long id) {
        final LocalStorage localStorage = (LocalStorage)this.getById((Serializable)id);
        return (LocalStorageDto)this.generator.convert(localStorage, (Class)LocalStorageDto.class);
    }
    
    public LocalStorageDto create(String name, final MultipartFile multipartFile) {
        FileUtil.checkSize(this.maxSize, multipartFile.getSize());
        final String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
        final String type = FileUtil.getFileType(suffix);
        final File file = FileUtil.upload(multipartFile, this.path + type + File.separator);
        if (ObjectUtil.isNull(file)) {
            throw new BadRequestException("上传失败");
        }
        try {
            name = (StringUtils.isBlank((CharSequence)name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name);
            final LocalStorage localStorage = new LocalStorage(file.getName(), name, suffix, file.getPath(), type, FileUtil.getSize(multipartFile.getSize()), "yshop");
            this.save(localStorage);
            return (LocalStorageDto)this.generator.convert(localStorage, (Class)LocalStorageDto.class);
        }
        catch (Exception e) {
            FileUtil.del(file);
            throw e;
        }
    }
    
    public void deleteAll(final Long[] ids) {
        for (final Long id : ids) {
            final LocalStorage storage = (LocalStorage)this.getById((Serializable)id);
            FileUtil.del(storage.getPath());
            this.removeById((Serializable)id);
        }
    }
    
    public void download(final List<LocalStorageDto> all, final HttpServletResponse response) throws IOException {
        final List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (final LocalStorageDto localStorage : all) {
            final Map<String, Object> map = new LinkedHashMap<String, Object>();
            map.put("文件真实的名称", localStorage.getRealName());
            map.put("文件名", localStorage.getName());
            map.put("后缀", localStorage.getSuffix());
            map.put("类型", localStorage.getType());
            map.put("大小", localStorage.getSize());
            map.put("操作人", localStorage.getOperate());
            map.put("创建日期", localStorage.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel((List)list, response);
    }
    
    public void updateLocalStorage(final LocalStorageDto resources) {
        final LocalStorage localStorage = (LocalStorage)this.getById((Serializable)resources.getId());
        BeanUtils.copyProperties(resources, localStorage);
        this.saveOrUpdate(localStorage);
    }
}
